package com.aos.erp.manager.rest.school;

import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.constant.GlobalConstant;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.manager.enums.sys.DepartmentType;
import com.aos.erp.manager.enums.sys.SchoolType;
import com.aos.erp.manager.enums.sys.UserType;
import com.aos.erp.manager.model.sys.dto.UserDto;
import com.aos.erp.manager.model.sys.vo.UserVo;
import com.aos.erp.manager.service.sys.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description: 后台用户
 * @author: tanzhiyu
 * @create: 2018-10-31 11:27
 **/
@Api(value = "驾校后台用户", tags = {"后台用户API"})
@RestController("RESTSchoolUserController")
@RequestMapping("/api/school/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    GlobalConstant globalConstant;
    /**
     * 新增用户
     *
     * @param userVo
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户账号", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "depCode", value = "部门组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "positionId", value = "岗位id", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "expireDatetime", value = "用户到期时间", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping(value = "/addUser")
    public ResultEntity addUser(UserVo userVo, String currentUserId) throws Exception {
        if (StringUtils.isEmpty(currentUserId)) {
            throw new BizException("请重新登录");
        }
        if (StringUtils.isBlank(userVo.getUsername())) {
            return ResultEntity.failed("用户账号不能为空", ResultEntity.CODE_NULL);
        }
        if (StringUtils.isBlank(userVo.getDepCode().toString())) {
            return ResultEntity.failed("部门组织code不能为空", ResultEntity.CODE_NULL);
        }
        if (StringUtils.isBlank(userVo.getPositionId().toString())) {
            return ResultEntity.failed("岗位id不能为空", ResultEntity.CODE_NULL);
        }
        //TODO
        Long operatorId = Long.valueOf(currentUserId);//操作人
        userVo.setOperatorId(operatorId);
        Map<String, Object> map = userService.addBeanSchool(userVo,globalConstant.getAPP_CODE_SCHOOL());
        return ResultEntity.success(map, "保存成功");
    }

    @ApiOperation(value = "修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "depCode", value = "部门组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "positionId", value = "岗位id", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "expireDatetime", value = "用户到期时间", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/updateUser")
    public ResultEntity updateUser(UserVo userVo, String currentUserId, String userId) throws Exception {
        if (StringUtils.isEmpty(currentUserId)) {
            throw new BizException("请重新登录");
        }
        if (StringUtils.isBlank(userVo.getId().toString())) {
            return ResultEntity.failed("用户ID不能为空", ResultEntity.CODE_NULL);
        }
        if (StringUtils.isBlank(userVo.getDepCode().toString())) {
            return ResultEntity.failed("部门组织code不能为空", ResultEntity.CODE_NULL);
        }
        if (StringUtils.isBlank(userVo.getPositionId().toString())) {
            return ResultEntity.failed("岗位id不能为空", ResultEntity.CODE_NULL);
        }
        //TODO
        Long operatorId = Long.valueOf(currentUserId);
        userVo.setOperatorId(operatorId);
        int i = userService.updateObjectSchool(userVo,globalConstant.getAPP_CODE_SCHOOL());
        if (i > 0) {
            return ResultEntity.success("修改成功");
        } else {
            return ResultEntity.success("修改失败");
        }
    }

    @ApiOperation(value = "删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/delUser")
    public ResultEntity delUser(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("用户ID不能为空", ResultEntity.CODE_NULL);
        }
        int i = userService.isDel(id,globalConstant.getAPP_CODE_SCHOOL());
        if (i > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.success("删除失败");
        }
    }

    @ApiOperation(value = "变更用户账号状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "state", value = "用户状态", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/updateUserState")
    public ResultEntity updateUserState(String id, String state) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("用户ID不能为空", ResultEntity.CODE_NULL);
        }
        if (StringUtils.isBlank(state)) {
            return ResultEntity.failed("用户状态不能为空", ResultEntity.CODE_NULL);
        }
        int i = userService.updateUserState(id, state);
        if (i > 0) {
            return ResultEntity.success("保存成功");
        } else {
            return ResultEntity.success("保存失败");
        }
    }

    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = " 每页显示条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "username", value = "用户账号", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "depName", value = "驾校名称", required = false, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "userSate", value = "用户状态", required = false, dataType = "String", paramType = "Query")
    })
    @PostMapping("/list")
    public ResultEntity<IPage<UserDto>> queryPageBean(@RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(name = "name", required = false) String name,
                                                      @RequestParam(name = "username", required = false) String username,
                                                      @RequestParam(name = "depName", required = false) String depName,
                                                      @RequestParam(name = "userSate", required = false) String userSate) throws Exception {

        return ResultEntity.success(userService.queryByPage(pageIndex, pageSize, null, name, username, depName, UserType.school.getValue().toString(), userSate));
    }

    @ApiOperation(value = "根据用户id查询")
    @PostMapping("/getUserId")
    public ResultEntity getUserId(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return ResultEntity.failed("用户id不能为空", ResultEntity.CODE_NULL);
        }
        UserDto userDto = userService.findByIdSchool(Long.valueOf(id));
        return ResultEntity.success(userDto, "获取用户信息");
    }

    @ApiOperation(value = "根据驾校组织code查询用户账号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depCode", value = "驾校组织code", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = " 类别（1：直营总校  2：非直营总校  3:分校 4：报名点）", dataType = "int", paramType = "query")
    })
    @PostMapping("/getUserSchool")
    public ResultEntity getUserSchool(String depCode, String type) throws Exception {
        if (StringUtils.isBlank(depCode) || StringUtils.isBlank(type)) {
            return ResultEntity.failed("驾校组织code或驾校类别不能为空", ResultEntity.CODE_NULL);
        }
        Map<String, Object> map = userService.selectByParams(depCode, type, null);
        return ResultEntity.success(map, "获取驾校用户账号信息");
    }
    @ApiOperation(value = "根据驾校组织code查询用户账号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = " 每页显示条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "depCode", value = "驾校组织code", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = " 类别（1：直营总校  2：非直营总校  3:分校 4：报名点）", dataType = "int", paramType = "query")
    })
    @GetMapping("/getUserSchoolPage")
    public ResultEntity<IPage<UserDto>>  getUserSchoolPage(@RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           String depCode, String type) throws Exception {
        if (StringUtils.isBlank(depCode) || StringUtils.isBlank(type)) {
            return ResultEntity.failed("驾校组织code或驾校类别不能为空", ResultEntity.CODE_NULL);
        }
        return ResultEntity.success(userService.queryByPage(pageIndex,pageSize,depCode, type, null));
    }
}
